package com.zwb.boot.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwb.boot.common.exception.CustomException;
import com.zwb.boot.entity.*;
import com.zwb.boot.mapper.OrdersMapper;
import com.zwb.boot.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author 头发又黑又长
 * @Date 2022/7/26 19:14
 * @email zwb15083976291@163.com
 */
@Slf4j
@Service
@Transactional
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
        implements OrdersService {

    @Autowired
    private UserService userService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 用户下单
     *
     * @param orders
     */
    @Override
    public void submit(Orders orders) {

        // 查询购物车信息
        List<ShoppingCart> shoppingCartList = shoppingCartService.listByUserId(orders.getUserId());
        if (ObjectUtils.isEmpty(shoppingCartList)) {
            throw new CustomException("购物车为空，不能下单");
        }
        // 获取用户信息
        User user = userService.getById(orders.getUserId());
        // 获取 地址信息
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        if (ObjectUtils.isEmpty(addressBook)) {
            throw new CustomException("地址为空，不能下单");
        }

        // 订单号
        long orderId = IdWorker.getId();
        // 原子整数
        AtomicInteger amount = new AtomicInteger(0);
        // 计算总金额 获得订单明细
        List<OrderDetail> orderDetails = shoppingCartList.stream().map(item -> {
            // 累加金额
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderId);
            orderDetail.setNumber(item.getNumber());
            orderDetail.setDishFlavor(item.getDishFlavor());
            orderDetail.setDishId(item.getDishId());
            orderDetail.setSetmealId(item.getSetmealId());
            orderDetail.setName(item.getName());
            orderDetail.setImage(item.getImage());
            orderDetail.setAmount(item.getAmount());
            // 添加到总金额
            amount.getAndAdd(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
            return orderDetail;
        }).collect(Collectors.toList());

        // 给订单表插入数据
        orders.setId(orderId);
        orders.setOrderTime(new Date());
        orders.setCheckoutTime(new Date());
        orders.setStatus(2); //订单状态
        orders.setAmount(new BigDecimal(amount.get())); // 金额
        orders.setUserId(user.getId());
        orders.setNumber(String.valueOf(orderId)); // 设置订单号
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());
        orders.setPhone(user.getPhone());
        orders.setAddress(addressBook.getDetail());
        this.save(orders);

        // 订单明细表插入数据
        orderDetailService.saveBatch(orderDetails);
        // 删除购物车中的数据
        shoppingCartService.removeBatchByIds(shoppingCartList.stream().map(ShoppingCart::getId).collect(Collectors.toList()));

    }
}
